package com.hhwy.framework.container;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import com.hhwy.framework.datasource.DataSourceType;
import com.hhwy.framework.datasource.DatasourceHelper;
import com.hhwy.framework.datasource.DynamicDataSource;
import com.hhwy.framework.datasource.domain.DatasourceConfig;
import com.hhwy.framework.datasource.service.DatasourceService;
/**
 * <b>类 名 称：</b>AppContext <br/>
 * <b>类 描 述：</b>核心容器，管理实体、逻辑对象的生命周期<br/>
 * <b>创 建 人：</b>lihuiming<br/>
 * <b>修 改 人：</b>Administrator<br/>
 * <b>修改时间：</b>2014年8月4日 上午9:47:12<br/>
 * <b>修改备注：</b><br/>
 * @version 1.0.0<br/>
 */
@Configuration
@EnableAspectJAutoProxy
public class AppContext {
	
	static Logger log = LoggerFactory.getLogger(AppContext.class);
	
	/**
	 * dataSource(动态数据源对象, 除默认数据源从db.properties读取外，其他数据源都从T_DATASOURCE_CONFIG表中读取)<br/>
	 * @return 
	 * DataSource
	 * @exception 
	 * @since  1.0.0
	 */
	@Bean
	public DynamicDataSource dataSource() {
		DynamicDataSource dds = new DynamicDataSource();
		Map<String, DataSource> datasourceMap = new HashMap<String, DataSource>();
		Map<String, DatasourceConfig> datasourceConfigMap = new HashMap<String, DatasourceConfig>();
		datasourceMap.put(DataSourceType.dataSource, defaultDataSource());

		DatasourceService datasourceService = new DatasourceService();
		if(!datasourceService.existsTDatasourceConfigTable()){
			datasourceService.createTDatasourceConfigTable();
		}
		List<DatasourceConfig> dcList = datasourceService.getAllConfig();
		for (DatasourceConfig dc : dcList) {
			datasourceMap.put(dc.getAppName(), DatasourceHelper.getDataSource(dc));
			datasourceConfigMap.put(dc.getAppName(),dc);
		}
		log.info("连接池总数："+datasourceMap.size()+" ["+getString(datasourceMap.keySet())+"]");
		dds.setTargetDataSourceConfigs(datasourceConfigMap);
		dds.setTargetDataSources(datasourceMap);

		dds.setDefaultTargetDataSource(datasourceMap.get(DataSourceType.dataSource));

		return dds;
	}
	private static String getString(Set<String> s) {
		StringBuffer sb = new StringBuffer();
		for(String k : s)
			sb.append(k).append(",");
		
		return sb.substring(0,sb.length()-1).toString();
	}
	
	@Bean
	@Primary
	public DataSource defaultDataSource(){
		
		DataSource dds = DatasourceHelper.getDefaultDataSource();

		return dds;
	}
	
	/**
	 * freeMarkerConfigurer(freemarker模板视图配置)<br/>
	 * properties所有属性
	 * DEFAULT_ENCODING_KEY_SNAKE_CASE = "default_encoding"; 
	 * LOCALIZED_LOOKUP_KEY_SNAKE_CASE = "localized_lookup";
	 * STRICT_SYNTAX_KEY_SNAKE_CASE = "strict_syntax";
	 * WHITESPACE_STRIPPING_KEY_SNAKE_CASE = "whitespace_stripping";
	 * CACHE_STORAGE_KEY_SNAKE_CASE = "cache_storage";
	 * TEMPLATE_UPDATE_DELAY_KEY_SNAKE_CASE = "template_update_delay";
	 * AUTO_IMPORT_KEY_SNAKE_CASE = "auto_import";
	 * AUTO_INCLUDE_KEY_SNAKE_CASE = "auto_include";
	 * TAG_SYNTAX_KEY_SNAKE_CASE = "tag_syntax";
	 * NAMING_CONVENTION_KEY_SNAKE_CASE = "naming_convention";
	 * TEMPLATE_LOADER_KEY_SNAKE_CASE = "template_loader";
	 * TEMPLATE_LOOKUP_STRATEGY_KEY_SNAKE_CASE = "template_lookup_strategy";
	 * TEMPLATE_NAME_FORMAT_KEY_SNAKE_CASE = "template_name_format";
	 * INCOMPATIBLE_IMPROVEMENTS_KEY_SNAKE_CASE = "incompatible_improvements";
	 * INCOMPATIBLE_ENHANCEMENTS = "incompatible_enhancements";
	 * @return 
	 * FreeMarkerConfigurer
	 * @exception 
	 * @since  1.0.0
	 */
	/*@Bean
	public FreeMarkerConfigurer freeMarkerConfigurer(){
		FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
		freeMarkerConfigurer.setTemplateLoaderPaths(new String[]{"/", "/plugins"});
		freeMarkerConfigurer.setDefaultEncoding("UTF-8");
		Properties settings = new Properties();
		settings.put("template_update_delay", "0");
		settings.put("default_encoding", "UTF-8");
		settings.put("whitespace_stripping", "true");
//		settings.put("auto_import", "index.ftl");
		freeMarkerConfigurer.setFreemarkerSettings(settings);
		return freeMarkerConfigurer;
	}*/
	
	/**
	 * freeMarkerViewResolver(freemarker视图解释器)<br/>
	 * @return 
	 * FreeMarkerViewResolver
	 * @exception 
	 * @since  1.0.0
	 */
	/*@Bean
	public FreeMarkerViewResolver freeMarkerViewResolver(){
		FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
		freeMarkerViewResolver.setContentType("text/html;charset=UTF-8");
		freeMarkerViewResolver.setViewClass(org.springframework.web.servlet.view.freemarker.FreeMarkerView.class);
		freeMarkerViewResolver.setOrder(1);
		return freeMarkerViewResolver;
	}*/
	
	/**
	 * internalResourceViewResolver(jsp视图解释器)<br/>
	 * @return 
	 * InternalResourceViewResolver
	 * @exception 
	 * @since  1.0.0
	 */
	/*@Bean
	public InternalResourceViewResolver internalResourceViewResolver(){
		InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
		internalResourceViewResolver.setContentType("text/html; charset=UTF-8");
		internalResourceViewResolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
		internalResourceViewResolver.setOrder(0);
		return internalResourceViewResolver;
	}*/
}
